﻿using DDD.Domain.Entities;
using DDD.Domain.Enums;
using DDD.Domain.ValueObjects;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace DDD.Infrastructure.Configurations
{
    internal sealed class UserConfiguration : IEntityTypeConfiguration<User>
    {
        public void Configure(EntityTypeBuilder<User> builder)
        {
            builder.ToTable(nameof(User));

            builder.HasKey(x => x.Id);
            builder.Property(x => x.Id).HasComment("主键").IsRequired().ValueGeneratedNever();

            builder.Property(c => c.UserName).HasComment("用户名").IsRequired();
            builder.HasIndex(c => c.UserName).IsUnique();

            builder.Property(c => c.Phone).HasComment("手机号").HasMaxLength(15).IsRequired(false);
            builder.Property(c => c.Email).HasComment("邮件").HasMaxLength(20).IsRequired(false);
            builder.Property(c => c.Password).HasComment("密码").HasMaxLength(20).IsRequired(true);
            builder.Property(c => c.Sex).HasComment("性别").IsRequired(true).HasDefaultValue(SexEnum.UnKnow);

            builder.OwnsOne(c => c.Address, entity =>
            {
                entity.WithOwner();

                entity.Property(c => c.Country).HasColumnName(nameof(Address.Country)).HasMaxLength(10).HasComment("国家").HasDefaultValue("中国");
                entity.Property(c => c.City).HasColumnName(nameof(Address.City)).HasMaxLength(10).HasComment("城市").IsRequired(false);
                entity.Property(c => c.Street).HasMaxLength(100).HasColumnName(nameof(Address.Street)).HasComment("街道").IsRequired(false);
            });

        }
    }
}
